﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Web.group
{
    public partial class employeeedit : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(Request["employeeid"]))
            {
                throw new Base.CustomException("缺少参数employeeid");
            }

            Hashtable employee = PhoneBook.Group.Employee.get(Request["employeeid"]);
            if (employee == null)
            {
                throw new Base.CustomException("参数employeeid不正确");
            }

            Hashtable user = PhoneBook.Users.Users.get(Commons.StrUtil.FormatValue(Session["userid"]));
            if (!user["groupid"].Equals(employee["groupid"]))
            {
                throw new Base.CustomException("无权限修改此员工");
            }

            if (!IsPostBack)
            {
                DataSet ds = new DataSet();

                if (user["IsBOOper"] != DBNull.Value && Convert.ToBoolean(user["IsBOOper"]))
                {
                    Hashtable bo = PhoneBook.Group.BranchOffice.get(Commons.StrUtil.FormatValue(user["bo"]));

                    ddlBo.Items.Add(new ListItem(Commons.StrUtil.FormatValue(bo["boname"]), Commons.StrUtil.FormatValue(bo["id"])));
                }
                else
                {
                    ds = PhoneBook.Group.BranchOffice.list(Commons.StrUtil.FormatValue(user["groupid"]));
                    Commons.ControlUtil.BindDDL(ddlBo, ds, "boname", "id", "非分公司", "");
                }

                txb_employeename.Text = Commons.StrUtil.FormatValue(employee["employeename"]);
                txb_age.Text = Commons.StrUtil.FormatValue(employee["age"]);
                txb_mobile.Text = Commons.StrUtil.FormatValue(employee["mobile"]);
                txb_phone.Text = Commons.StrUtil.FormatValue(employee["phone"]);
                txb_email.Text = Commons.StrUtil.FormatValue(employee["email"]);
                txb_shortnum.Text = Commons.StrUtil.FormatValue(employee["shortnum"]);

                txb_qq.Text = Commons.StrUtil.FormatValue(employee["qq"]);
                txb_msn.Text = Commons.StrUtil.FormatValue(employee["msn"]);
                txb_fax.Text = Commons.StrUtil.FormatValue(employee["fax"]);
                txb_duty.Text = Commons.StrUtil.FormatValue(employee["duty"]);

                if (ddlBo.Items.FindByValue(Commons.StrUtil.FormatValue(employee["boid"])) != null)
                    ddlBo.SelectedValue = Commons.StrUtil.FormatValue(employee["boid"]);

                if (string.IsNullOrWhiteSpace(ddlBo.SelectedValue))
                {
                    ds = PhoneBook.Group.Department.listforunit(Commons.StrUtil.FormatValue(user["groupid"]));
                }
                else
                {
                    ds = PhoneBook.Group.Department.list(Commons.StrUtil.FormatValue(user["groupid"]), employee["boid"].ToString());
                }
                Commons.ControlUtil.BindDDL(ddl_department, ds, "departmentname", "departmentid", "请选择部门", "");

                if (ddl_department.Items.FindByValue(Commons.StrUtil.FormatValue(employee["departmentid"])) != null)
                    ddl_department.SelectedValue = Commons.StrUtil.FormatValue(employee["departmentid"]);
            }
        }

        protected void btn_save_Click(object sender, EventArgs e)
        {
            if (ddlBo.SelectedValue == "")
            {
                Helper.ShowMessage(u_message, controls.UCMessageType.error, "请选择所属分公司");
                return;
            }
            Hashtable user = PhoneBook.Users.Users.get(Commons.StrUtil.FormatValue(Session["userid"]));

            Hashtable employee = PhoneBook.Group.Employee.get(Request["employeeid"]);

            addLog(employee);

            employee["employeename"] = txb_employeename.Text;
            employee["age"] = txb_age.Text;
            employee["departmentid"] = ddl_department.SelectedValue;
            employee["mobile"] = txb_mobile.Text;
            employee["phone"] = txb_phone.Text;
            employee["email"] = txb_email.Text;
            employee["shortnum"] = txb_shortnum.Text;

            employee["qq"] = txb_qq.Text;
            employee["msn"] = txb_msn.Text;
            employee["fax"] = txb_fax.Text;
            employee["duty"] = txb_duty.Text;
            employee["boid"] = ddlBo.SelectedValue;

            PhoneBook.Group.Employee.edit(employee);
            Helper.ShowMessage(u_message, controls.UCMessageType.success, "保存成功");
        }

        //添加日志
        private void addLog(Hashtable employee)
        {
            string content = string.Empty;

            if (employee["employeename"].ToString() != txb_employeename.Text)
                content += string.Format("姓名:{0}->{1};", employee["employeename"], txb_employeename.Text);

            if (employee["age"].ToString() != txb_age.Text)
                content += string.Format("年龄:{0}->{1};", employee["age"], txb_age.Text);

            if (Commons.StrUtil.FormatValue(employee["boid"]) != ddlBo.SelectedValue)
            {
                string strBoId = Commons.StrUtil.FormatValue(employee["boid"]);

                string strBoName = string.Empty;

                Hashtable bo = PhoneBook.Group.BranchOffice.get(strBoId);

                if (bo != null && bo.Count > 0)
                {
                    strBoName = Commons.StrUtil.FormatValue(bo["boname"]);
                }
                else
                {
                    strBoName = "非分公司";
                }
                
                content += string.Format("分公司:{0}->{1};", strBoName, ddlBo.SelectedItem.Text);
            }

            if (employee["departmentid"].ToString() != ddl_department.SelectedValue)
                content += string.Format("部门变更为:{0};", ddl_department.SelectedItem.Text);

            if (employee["mobile"].ToString() != txb_mobile.Text)
                content += string.Format("手机号:{0}->{1};", employee["mobile"], txb_mobile.Text);

            if (employee["phone"].ToString() != txb_phone.Text)
                content += string.Format("固定电话:{0}->{1};", employee["phone"], txb_phone.Text);

            if (employee["email"].ToString() != txb_email.Text)
                content += string.Format("邮箱:{0}->{1};", employee["email"], txb_email.Text);

            if (employee["shortnum"].ToString() != txb_shortnum.Text)
                content += string.Format("短号码:{0}->{1};", employee["shortnum"], txb_shortnum.Text);

            if (employee["qq"].ToString() != txb_qq.Text)
                content += string.Format("QQ:{0}->{1};", employee["qq"], txb_qq.Text);

            if (employee["msn"].ToString() != txb_msn.Text)
                content += string.Format("MSN:{0}->{1};", employee["msn"], txb_msn.Text);

            if (employee["fax"].ToString() != txb_fax.Text)
                content += string.Format("传真:{0}->{1};", employee["fax"], txb_fax.Text);

            if (employee["duty"].ToString() != txb_duty.Text)
                content += string.Format("职务:{0}->{1};", employee["duty"], txb_duty.Text);

            if (!string.IsNullOrEmpty(content))
            {
                content = string.Format("修改了人员信息({0})。{1}", employee["employeename"], content);

                AddLog(content);
            }
        }

        protected void ddlBo_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(ddlBo.SelectedValue))
            {
                Hashtable user = PhoneBook.Users.Users.get(Commons.StrUtil.FormatValue(Session["userid"]));
                DataSet ds = PhoneBook.Group.Department.listforunit(Commons.StrUtil.FormatValue(user["groupid"]));
                Commons.ControlUtil.BindDDL(ddl_department, ds, "departmentname", "departmentid", "请选择部门", "");
            }
            else
            {
                Hashtable user = PhoneBook.Users.Users.get(Commons.StrUtil.FormatValue(Session["userid"]));
                DataSet ds = PhoneBook.Group.Department.list(Commons.StrUtil.FormatValue(user["groupid"]), ddlBo.SelectedValue);
                Commons.ControlUtil.BindDDL(ddl_department, ds, "departmentname", "departmentid", "请选择部门", "");
            }
        }
    }
}